package top.boycharse.ftp.client.common;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class DataDTPClient {

    private ServerSocket serverSocket;

    public DataDTPClient(ServerSocket serverSocket) {
        this.serverSocket = serverSocket;
    }

    private void connect(ServerSocket serverSocket,Strategy strategy) {
        Socket socket = null;
        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            socket = serverSocket.accept();
            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();
            if (strategy != null) {
                strategy.op(outputStream,inputStream);
            }
        }catch (Exception e) {
            if (strategy != null) {
                strategy.exception(e);
            }
        }finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void upload(File file) {
        connect(serverSocket, new Strategy() {
            @Override
            public void op(OutputStream outputStream, InputStream inputStream) {
                FileInputStream fileInputStream = null;
                try {
                    fileInputStream = new FileInputStream(file);
                    byte[] b = new byte[1024];
                    int len = -1;
                    int total = 0;
                    while ((len = fileInputStream.read(b)) != -1) {
                        outputStream.write(b,0,len);
                        outputStream.flush();
                        total += len;
                        System.out.println("已上传" + len);
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void exception(Exception e) {

            }
        });
    }

    public List<String> getFileList() {
        List<String> fileList = new ArrayList<>();
        connect(serverSocket, new Strategy() {
            @Override
            public void op(OutputStream outputStream, InputStream inputStream) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String str = null;
                try {
                    while ((str = reader.readLine()) != null) {
                        fileList.add(str);
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void exception(Exception e) {

            }
        });
        return fileList;
    }

    public void download(String filename) {
        Socket socket = null;
        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            socket = serverSocket.accept();
            File file = new File(filename);
            if (!file.exists() || !file.isFile()) {
                file.createNewFile();
            }
            System.out.println(file.getAbsoluteFile());
            outputStream = new FileOutputStream(file);
            inputStream = socket.getInputStream();
            byte[] b = new byte[1024];
            int len = -1;
            int total = 0;
            while ((len = inputStream.read(b)) != -1) {
                outputStream.write(b,0, len);
                total += len;
                System.out.println(total);
            }
            outputStream.flush();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public interface Strategy {
        void op(OutputStream outputStream,InputStream inputStream);

        void exception(Exception e);
    }
}
